﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics.PackedVector;

namespace ProceduralGolf
{
    class MiniMap
    {
        GolfCourseLayout layout;
        Texture2D layoutTex;
        Game game;
        SpriteBatch spriteBatch;
        public Vector2 Position { get; set; }
        Vector2 ballPos;
        float ballRadius = 2.5f;
        Vector2 offset;

        float mapScale = 0.5f;
        public float Width { get { return layoutTex.Width * mapScale; } }
        public float Height { get { return layoutTex.Height * mapScale; } }

        private Texture2D ballTex;

        TextureCircleDrawer circledrawer;

        public MiniMap(Game game, GolfCourseLayout layout)
        {
            this.game = game;
            this.layout = layout;
            this.layoutTex = layout.getLayout();
            spriteBatch = new SpriteBatch(game.GraphicsDevice);

            circledrawer = new TextureCircleDrawer(game);

            offset = (layout.TerrainSize - layout.CourseSize)/2;
            ballTex = new Texture2D(game.GraphicsDevice, (int)(2*ballRadius), (int)(2*ballRadius), 1, TextureUsage.None, SurfaceFormat.Color);
            ballTex = circledrawer.draw(ballTex, new Circle(new Vector2(ballRadius, ballRadius), ballRadius), Color.White);
        }

        ~MiniMap()
        {
            spriteBatch.Dispose();
            if (!layoutTex.IsDisposed)
                layoutTex.Dispose();
            if (!ballTex.IsDisposed)
                ballTex.Dispose();
        }

        public void SetBallPosition(Vector2 newBallPos)
        {
            this.ballPos = newBallPos - offset;
        }

        public void Draw()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(layoutTex, Position, null, Color.White, 0, Vector2.Zero, 0.5f, SpriteEffects.None, 1);
            spriteBatch.Draw(ballTex, Position + (ballPos * mapScale) - new Vector2(ballRadius, ballRadius), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1);
            spriteBatch.End();
        }
    }
}
